<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Final_Results_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('result_model', '', true);
        $this->load->model('user', '', true);
        if (!$this->session->userdata('logged_in')) {
            redirect('login', 'refresh');
        }

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('HR', 'SC'));
    }

    function index($region = 'all', $title = 'all', $ss = 'all') {

        $this->load->model('evaluation', '', true);
        $evaluation = $this->evaluation->get_last();

        // lấy toàn bộ danh sách nhân viên, trừ ss trở lên
        //$titles = $this->user->get_enum_values();
        $titles = array('NR' => 'NR', 'PG' => 'PG', 'BS' => 'BS', 'Medical' => 'Medical');
        $viewData['titles'] = $titles;

        $this->load->model('region');
        $regions = $this->region->get_list();
        $viewData['regions'] = $regions;

        $regionDic = array();
        foreach ($regions as $key => $value) {
            $regionDic[$value['rsm_code']] = $value['region_name'];
        }
        $viewData['regionDic'] = $regionDic;
        
        // danh sách SS
        $ssList = $this->user->get_list_by_title(array('SS', 'KAS'), array('status' => 'active'));
        $viewData['ssList'] = $ssList;

        $users = array();
        if ($title != 'all') {
            $viewData['filterTitle'] = $title;
            switch ($title) {
                case 'NR':
                    $users = $this->user->get_list_by_title(array('NR', 'NRTL'), array('status' => 'active'));
                    break;
                case 'PG':
                    $users = $this->user->get_list_by_title(array('PG', 'PGTL'), array('status' => 'active'));
                    break;
                case 'BS':
                    $users = $this->user->get_list_by_title(array('BS', 'BSTL'), array('status' => 'active'));
                    break;
                case 'Medical': $users = $this->user->get_list_by_title(array('MedNR', 'MedBS', 'MedTL'), array('status' => 'active'));
                    break;
            }
        } else {
            $users = $this->user->get_list_by_title(get_employee_titles(), array('status' => 'active'));
        }
        if ($region != 'all') {
            $temp = array();
            foreach ($users as $eachUser) {
                if ($eachUser['rsm_code'] == $region) {
                    $temp[] = $eachUser;
                }
            }
            $users = $temp;
            $viewData['filterRegion'] = $region;
        }
        if ($ss != 'all') {
            $temp = array();
            foreach ($users as $eachUser) {
                if ($eachUser['supervisor_code'] == $ss) {
                    $temp[] = $eachUser;
                }
            }
            $users = $temp;
            $viewData['filterSS'] = $ss;
        }
        
        $stat = array();
        $stat2 = array();
        get_final_results($users, $evaluation['id'], $stat, $stat2);
        
        $this->load->model('Settings');
        $settings = $this->Settings->get(1);
        $viewData['settings'] = $settings;
       
        // hiển thị
        $viewData['rows'] = $users;
        $viewData['ni'] = $stat['ni'];
        $viewData['be'] = $stat['be'];
        $viewData['ef'] = $stat['ef'];
        $viewData['he'] = $stat['he'];
        $viewData['ex'] = $stat['ex'];

        $chartData = array();
        $chartData[] = array('Loại', 'MEKONG', 'NORTH', 'MT', 'CENTRAL', 'MEDICAL', 'SOUTH EAST', 'HCM');
        $chartData[] = array('NI', $stat2['nimek'], $stat2['ninorth'], $stat2['nimt'], $stat2['nicentral'], $stat2['nimedical'], $stat2['nise'], $stat2['nihcm']);
        $chartData[] = array('BE', $stat2['bemek'], $stat2['benorth'], $stat2['bemt'], $stat2['becentral'], $stat2['bemedical'], $stat2['bese'], $stat2['behcm']);
        $chartData[] = array('EF', $stat2['efmek'], $stat2['efnorth'], $stat2['efmt'], $stat2['efcentral'], $stat2['efmedical'], $stat2['efse'], $stat2['efhcm']);
        $chartData[] = array('HE', $stat2['hemek'], $stat2['henorth'], $stat2['hemt'], $stat2['hecentral'], $stat2['hemedical'], $stat2['hese'], $stat2['hehcm']);
        $chartData[] = array('EX', $stat2['exmek'], $stat2['exnorth'], $stat2['exmt'], $stat2['excentral'], $stat2['exmedical'], $stat2['exse'], $stat2['exhcm']);

        $viewData['chartData'] = json_encode($chartData);

        $this->load->view(get_folder($this->loggedUser->title) . '/final_results_list', $viewData);
    }


}